﻿package mortal.game.scene3D.player.item
{
    import Message.BroadCast.*;
    import Message.DB.Tables.*;
    import Message.Public.*;
    import baseEngine.modifiers.*;
    import com.gengine.debug.*;
    import com.greensock.*;
    import com.mui.controls.*;
    import extend.language.*;
    import flash.events.*;
    import flash.text.*;
    import flash.utils.*;
    import frEngine.*;
    import frEngine.event.*;
    import mortal.common.global.*;
    import mortal.game.*;
    import mortal.game.manager.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.scene3D.display3d.icon3d.*;
    import mortal.game.scene3D.display3d.text3d.dynamicText3d.*;
    import mortal.game.scene3D.layer3D.utils.*;
    import mortal.game.scene3D.model.player.*;
    import mortal.game.scene3D.model.pools.*;
    import mortal.game.scene3D.player.entity.*;
    import mortal.game.utils.*;
    import mortal.game.view.common.*;

    public class ItemPlayer extends Game2DPlayer
    {
        protected var _dropEntityInfo:SDropEntityInfo;
        protected var _dropIcon:String = "";
        protected var _dropItemColor:int;
        protected var _name:String;
        protected var icon:Icon3D;
        protected var _offsetX:uint = 0;
        protected var _offsetY:uint = 0;
        protected var _xnum:int = 1;
        protected var _ynum:int = 1;
        private var _textField:GTextFiled;
        private var _text3D:Text3D;
        private var _tween:TweenMax;
        private var _dropEffect:EffectPlayer;
        private var _flickerEffect:EffectPlayer;
        private var timeOutDisposeKey:int;
        public static var purpleEffectUrl:String = "jingbidiaoluo-xiyou-zi";
        public static var orangeEffectUrl:String = "jingbidiaoluo-xiyou-jin";
        public static var jinbiEffectUrl:String = "xiyoudiaoluo-diaoluo";
        public static var flickerEffectUrl:String = "jingbidiaoluo-jingbi";

        public function ItemPlayer()
        {
            overPriority = Game2DOverPriority.Drop;
            return;
        }// end function

        public function get dropEntityInfo() : SDropEntityInfo
        {
            return this._dropEntityInfo;
        }// end function

        private function timeoutDispose() : void
        {
            if (this._dropEntityInfo)
            {
                ThingUtil.dropItemUtil.removeDropItem(this._dropEntityInfo.entityId);
            }
            if (this.timeOutDisposeKey)
            {
                clearTimeout(this.timeOutDisposeKey);
                this.timeOutDisposeKey = 0;
            }
            return;
        }// end function

        public function set dropEntityInfo(param1:SDropEntityInfo) : void
        {
            var _loc_3:String = null;
            var _loc_4:int = 0;
            var _loc_5:ItemInfo = null;
            var _loc_6:TMoneyConfig = null;
            var _loc_7:Boolean = false;
            var _loc_8:String = null;
            var _loc_9:Array = null;
            var _loc_10:Array = null;
            var _loc_11:Array = null;
            var _loc_12:Number = NaN;
            var _loc_13:Number = NaN;
            this._dropEntityInfo = param1;
            this.x2d = param1.point.x;
            this.y2d = param1.point.y;
            if (!DropItemUtil.isCanSelfGet(param1))
            {
                _loc_4 = this._dropEntityInfo.dropDt.time + 60000 - ClockManager.instance.nowDate.time;
                if (_loc_4 > 0)
                {
                    this.timeOutDisposeKey = setTimeout(this.timeoutDispose, _loc_4);
                }
                else
                {
                    this.timeoutDispose();
                }
            }
            var _loc_2:* = param1.playerItems[0];
            this._dropItemColor = 0;
            if (_loc_2)
            {
                _loc_5 = ItemConfig.instance.getConfig(_loc_2.itemCode);
                _loc_3 = _loc_5.dropIcon;
                this._name = ItemsUtil.getItemName(new ItemData(_loc_2));
                this._dropItemColor = _loc_5.color;
            }
            else
            {
                this._name = Language.getString(20674);
                _loc_6 = MoneyConfig.instance.getInfoByUint(param1.unit);
                if (_loc_6)
                {
                    _loc_3 = _loc_6.icon;
                    this._name = GameDefConfig.instance.getEPrictUnitName(_loc_6.unit);
                }
            }
            if (_loc_3)
            {
                _loc_7 = _loc_3.indexOf(".swf") > -1;
                if (!_loc_7)
                {
                    _loc_8 = Icon3DFactory.instance.geticonId(_loc_3);
                    if (_loc_8)
                    {
                        _loc_9 = _loc_8.split("#");
                        _loc_10 = _loc_9[1].split("-");
                        _loc_11 = _loc_9[2].split("-");
                        this._offsetX = _loc_10[0];
                        this._offsetY = _loc_11[0];
                        this._xnum = _loc_10[1];
                        this._ynum = _loc_11[1];
                        this._dropIcon = _loc_9[0] + ".png";
                        this.addIcon3D(this._dropIcon, this._offsetX, this._offsetY, this.xnum, this.ynum);
                    }
                }
            }
            if (this.icon)
            {
                if (ClockManager.instance.nowDate.time - param1.dropDt.time > 2000)
                {
                    this.onPlayEnd();
                }
                else if (param1.unit == EPriceUnit._EPriceUnitCoin || param1.unit == EPriceUnit._EPriceUnitCoinBind)
                {
                    if (this._dropEffect)
                    {
                        FrEventDispatcher.instance.proxyRemoveEventListener(this._dropEffect, Engine3dEventName.PLAYEND, this.onPlayEnd);
                        this._dropEffect.dispose();
                    }
                    this._dropEffect = EffectPlayerPool.instance.getEffectPlayer(jinbiEffectUrl, Game.scene.playerLayer, null, true, true);
                    this._dropEffect.x2d = param1.point.x;
                    this._dropEffect.y2d = param1.point.y;
                    FrEventDispatcher.instance.proxyAddEventListener(this._dropEffect, Engine3dEventName.PLAYEND, this.onPlayEnd);
                    this._dropEffect.play(PlayMode.ANIMATION_STOP_MODE);
                }
                else
                {
                    this.addIconToStage();
                    _loc_12 = 0.8;
                    _loc_13 = 800;
                    this._tween = TweenMax.to(this.icon, _loc_12, {physicsProps:{y:{velocity:_loc_13, acceleration:(-_loc_13) * 2 / _loc_12}}, onComplete:this.onPlayEnd});
                }
            }
            return;
        }// end function

        private function onPlayEnd(event:Event = null) : void
        {
            this.addIconToStage();
            this.addTitle(this._name);
            if (this._flickerEffect)
            {
                this._flickerEffect.dispose();
            }
            var _loc_2:* = this.getColorUrl();
            if (_loc_2)
            {
                this._flickerEffect = EffectPlayerPool.instance.getEffectPlayer(_loc_2, Game.scene.playerLayer, null, true);
                this._flickerEffect.x2d = this._dropEntityInfo.point.x;
                this._flickerEffect.y2d = this._dropEntityInfo.point.y;
                this._flickerEffect.play(PlayMode.ANIMATION_LOOP_MODE);
            }
            else
            {
                this._flickerEffect = EffectPlayerPool.instance.getEffectPlayer(flickerEffectUrl, Game.scene.playerLayer, null, true);
                this._flickerEffect.x2d = this._dropEntityInfo.point.x;
                this._flickerEffect.y2d = this._dropEntityInfo.point.y;
                this._flickerEffect.play(PlayMode.ANIMATION_LOOP_MODE);
            }
            return;
        }// end function

        private function getColorUrl() : String
        {
            if (this._dropItemColor == 3)
            {
                return purpleEffectUrl;
            }
            if (this._dropItemColor == 4)
            {
                return orangeEffectUrl;
            }
            return "";
        }// end function

        protected function addIconToStage() : void
        {
            if (this.icon)
            {
                this.addChild(this.icon);
            }
            return;
        }// end function

        protected function addIcon3D(param1:String, param2:uint, param3:uint, param4:int, param5:int) : void
        {
            if (!this.icon)
            {
                Log.debug(param1, param2, param3);
                this.icon = Icon3DFactory.instance.createicon3D(param1, param2, param3, param4, param5);
            }
            return;
        }// end function

        protected function addTitle(param1:String) : void
        {
            if (!this._textField)
            {
                this._textField = UIFactory.textField("", 0, 0, -1, -1, null, GlobalStyle.textFormatItemWhite);
                this._textField.autoSize = TextFieldAutoSize.LEFT;
                this._textField.htmlText = param1;
                this._text3D = Text3DFactory.instance.createtext3D(this._textField, false, this);
            }
            else
            {
                this._textField.htmlText = param1;
                Text3DFactory.instance.updateText3D(this._text3D, this._textField, this);
            }
            this._text3D.x = (-this._text3D.textWidth) / 1.7;
            this._text3D.y = 20;
            return;
        }// end function

        override public function hoverTest(param1:Number, param2:Number, param3:Number, param4:Number) : Boolean
        {
            var _loc_5:* = this.xnum * 35;
            var _loc_6:* = this.ynum * 35;
            _displayRec.x = this.x2d - param1 - _loc_5 / 2;
            _displayRec.y = this.y2d - param2 - _loc_6 / 2;
            _displayRec.width = _loc_5;
            _displayRec.height = _loc_6;
            return _displayRec.contains(param3, param4);
        }// end function

        override public function dispose(param1:Boolean = true) : void
        {
            super.dispose(param1);
            if (this.timeOutDisposeKey)
            {
                clearTimeout(this.timeOutDisposeKey);
                this.timeOutDisposeKey = 0;
            }
            if (this._tween)
            {
                this._tween.kill();
                this._tween = null;
            }
            this._dropItemColor = 0;
            this._dropIcon = "";
            this._name = "";
            this._xnum = 1;
            this._ynum = 1;
            this._offsetX = 0;
            this._offsetY = 0;
            this._dropEntityInfo = null;
            if (this._dropEffect)
            {
                FrEventDispatcher.instance.proxyRemoveEventListener(this._dropEffect, Engine3dEventName.PLAYEND, this.onPlayEnd);
                this._dropEffect.dispose();
                this._dropEffect = null;
            }
            if (this._flickerEffect)
            {
                this._flickerEffect.dispose();
                this._flickerEffect = null;
            }
            if (this.icon)
            {
                Icon3DFactory.instance.disposeIcon3D(this.icon);
                this.icon = null;
            }
            if (this._text3D)
            {
                Text3DFactory.instance.disposeText3D(this._text3D);
                this._text3D = null;
            }
            if (this._textField)
            {
                this._textField.dispose();
                this._textField = null;
            }
            return;
        }// end function

        public function get dropIcon() : String
        {
            return this._dropIcon;
        }// end function

        public function get xnum() : int
        {
            return this._xnum;
        }// end function

        public function get ynum() : int
        {
            return this._ynum;
        }// end function

        public function get offsetX() : uint
        {
            return this._offsetX;
        }// end function

        public function get offsetY() : uint
        {
            return this._offsetY;
        }// end function

    }
}
